<?php

namespace App\Services;

use App\Models\Cms_Gift;
use App\Models\Cms_Gift_Record;
use App\Traits\External;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GiftService
{
    public function giftList($params)
    {
        $result = Cms_Gift::getFilter($params)
            ->Nodel()
            ->orderBy('created_at','desc')
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function insertGift($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $time = Carbon::now()->timestamp;
            $params['create_time']  = $time;
            $params['update_time']  = $time;
            $result = Cms_Gift::create($params);
            if(!$result)
                throw new \Exception("新增Cms_Gift失败".$params['id']);

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function updateGift($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $time = Carbon::now()->timestamp;
            $params['update_time']  = $time;
            $result = Cms_Gift::where('id',$params['id'])->update($params);
            if(!$result){
                throw new \Exception("更新Cms_Gift失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function giftDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Cms_Gift::find($params['id']);
            $res->is_del = 1;
            $res->update_time = Carbon::now()->timestamp;
            if(!$res->save()){
                throw new \Exception("删除Cms_Line失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function giftRecord($params)
    {
        $result = Cms_Gift_Record::getFilter($params)
            ->Nodel()
            ->orderBy('created_at','desc')
            ->paginate($params['pageSize']);
        foreach ($result as &$value) {
            $value->giftname = $value->hasOneGift->name;
            $value->username = $value->hasOneMember->username;
            $value->sn = $value->hasOneMember->sn;
            $value->othername = $value->hasOneOtherMember->username;
            $value->othersn = $value->hasOneOtherMember->sn;
        }
        $result->data = $result->makeHidden(['hasOneGift','hasOneMember','hasOneOtherMember']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function giftRecordDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();

            $res = Cms_Gift_Record::find($params['id']);
            $res->is_del = 1;
            $res->update_time = Carbon::now()->timestamp;
            if(!$res->save()){
                throw new \Exception("删除Cms_Gift_Record失败".$params['id']);
            }

            DB::commit();
            return true;
        }catch (\Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
